= render 'shared/header_big'
.w-row.admin.admin-contributions
  h1
    = @title = t('admin.management.title')
    ' :
  h3 = t('admin.filter.title')
  = simple_form_for '', url: admin_contributions_path, method: :get do |form|
    .w-row
      .w-col-2.w-col
        = form.input 'between_values[start_at]', as: :integer, label: t(".between_values")
      .w-col-2.w-col.no-label
        = form.input 'between_values[ends_at]', as: :integer, label: ''
      .w-col-3.w-col
        = form.input :user_email_contains, as: :string, label: t('.email')
      .w-col-2.w-col
        = form.input :by_payment_id, as: :string, label: t('.payment_id')
      .w-col-2.w-col
        = form.input :with_state, collection: Payment.state_names, label: t('admin.filter.with_state'), as: :select, selected: params[:with_state], include_blank: true
    .w-row
      .w-col-3.w-col
        = form.input :project_name_contains, as: :string, label: t('.project')
      .w-col-2.w-col
        = form.input :user_name_contains, as: :string, label: t('.user')
      .w-col-2.w-col
        = form.input :search_on_acquirer, as: :string, label: t('.acquirer'), input_html: { placeholder: t('.placeholders.acquirer'), title: t('.placeholders.acquirer'), value: current_scopes[:search_on_acquirer] }
      .w-col-2.w-col
        = form.input :by_gateway, collection: Payment.pluck("DISTINCT gateway"), label: t('admin.filter.by_gateway'), as: :select, selected: params[:by_gateway], include_blank: true
      .w-col-2.w-col
        = form.button :submit, t('admin.filter.submit'), class: 'button medium negative'

  = paginate @contributions
  - if @contributions.empty?
    = t('.no_contributions')
  - else
    - @contributions.each do |contribution|
      table.table.admin-contributions
        thead.card.card-secondary
          tr
            th= t('.contribution')
            th= t('.project')
            th= t('.project_state')
            th= t('.user')
            th= t('.value')
            th= t('.payment_tax')
            th= t('.reward')
            th= t('.emails')
            th= t('.anonymous')
            th= t('.credits')
            th= t('.last_state')
            th= t('.state')
            th
        tbody
          tr id=contribution.contribution_id
            td
              = link_to contribution.contribution_id, admin_contribution_path(contribution.id)
            td.project_info title=contribution.project.name
              = link_to project_by_slug_path(permalink: contribution.project.permalink) do
                = image_tag contribution.project.display_image, class: 'project_img'
            td.project_info
              = contribution.project.state
            td.user_info title=contribution.user.display_name
              = link_to edit_user_path(contribution.user) do
                = image_tag contribution.user.display_image, class: 'user_img'
              .user_id= best_in_place [:admin, contribution.contribution], :user_id, method: :put, type: :input
            td= contribution.value
            td= contribution.gateway_fee
            td.w-col-6.w-col.reward.project-events-dropdown title=("#{contribution.reward.display_minimum} - #{contribution.reward.description}" if contribution.reward)
              - if contribution.reward
                span = contribution.reward.display_minimum
              - else
                | -
              .w-dropdown-toggle.dropdown-toggle.project-admin-menu
                = link_to 'javascript:void(0);', data: {toggle: 'dropdown'}, class: 'w-icon-dropdown-toggle btn btn-dark round positive dropdown-toggle' do
                  span.caret
              nav.w-dropdown-list.dropdown-list
                = link_to t('.no_reward'), change_reward_admin_contribution_path(contribution, reward_id: nil), method: :put, class: 'w-dropdown-link dropdown-link fontsize-smaller'
                - contribution.available_rewards.each do |reward|
                  = link_to reward.minimum_value, change_reward_admin_contribution_path(contribution, reward_id: reward.id, local_params: params), method: :put, class: 'w-dropdown-link dropdown-link fontsize-smaller'

            td.key
              strong= t('.details.payer_email') 
              br/
              = contribution.payer_email
              br/
              strong= t('.details.normal_email')
              br/
              = contribution.user.email
            td.checkbox= best_in_place [:admin, contribution.contribution], :anonymous, method: :put, type: :checkbox, collection: [t('no'), t('yes')]
            td= (contribution.credits? ? t('yes') : t('no'))
            td= contribution.last_state_name
            td= contribution.state
            td.w-col-6.w-col
              .w-dropdown-toggle.dropdown-toggle.project-admin-menu
                = link_to 'javascript:void(0);', data: {toggle: 'dropdown'}, class: 'w-icon-dropdown-toggle btn btn-dark round positive dropdown-toggle' do
                  span.caret
              nav.w-dropdown-list.dropdown-list
                - unless %w[paid refunded].include?(contribution.state)
                  = link_to t('admin.contributions.index.confirm_contribution'), pay_admin_contribution_path(contribution, local_params: params), method: :put, class: 'w-dropdown-link dropdown-link fontsize-smaller'

                - if contribution.paid?
                  = link_to t('admin.contributions.index.request_refund'), request_refund_admin_contribution_path(contribution, local_params: params), method: :put, class: 'w-dropdown-link dropdown-link fontsize-smaller'
                - if contribution.paid? || contribution.deleted? || contribution.pending_refund? || contribution.refunded?
                  = link_to t('admin.contributions.index.direct_refund'), gateway_refund_admin_contribution_path(contribution, local_params: params), method: :put, class: 'w-dropdown-link dropdown-link fontsize-smaller'

                - if contribution.pending_refund? || contribution.paid? || contribution.deleted?
                  = link_to t('admin.contributions.index.refund_contribution'), refund_admin_contribution_path(contribution, local_params: params), method: :put, class: 'w-dropdown-link dropdown-link fontsize-smaller'

                - if contribution.paid?
                  = link_to t('admin.contributions.index.chargeback'), chargeback_admin_contribution_path(contribution, local_params: params), method: :put, class: 'w-dropdown-link dropdown-link fontsize-smaller'

                - unless contribution.paid?
                  = link_to t('admin.contributions.index.push_to_trash'), trash_admin_contribution_path(contribution, local_params: params), method: :put, class: 'w-dropdown-link dropdown-link fontsize-smaller'

                - if contribution.second_slip_path
                  = link_to t('admin.contributions.index.second_slip'), contribution.second_slip_path, class: 'w-dropdown-link dropdown-link fontsize-smaller', target: :blank

          tr class='detail_tr' id="contribution_details-#{contribution.id}"
            td colspan='11'
              table.table style='border: 1px #ededed solid; margin-bottom: 30px'
                thead
                  th= t('.key')
                  th= t('.details.payment_id')
                  th= t('.details.payment_method')
                  th= t('.details.acquirer_tid')
                  th= t('.details.acquirer_name')
                  th= t('.details.payment_choice')
                  th= t('.details.installments')
                  th= t('.details.created_at')
                tbody
                  tr
                    td= contribution.key
                    td= contribution.gateway_id
                    td= contribution.gateway
                    - if contribution.gateway_data
                      td= contribution.gateway_data["acquirer_tid"]
                      td= contribution.gateway_data["acquirer_name"]
                    - else
                      td &nbsp;
                      td &nbsp;
                    td= contribution.payment_method
                    td= contribution.installments
                    td= l(contribution.created_at, format: :simple) if contribution.created_at.present?
= paginate @contributions

= render 'shared/footer_big'
